package com.steven.im.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.steven.im.R;
import com.steven.im.entity.IMContacts;

import java.util.ArrayList;

public class IMGroupListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<IMContacts> list;

    public IMGroupListAdapter(Context context, ArrayList<IMContacts> list) {
        super();
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_group, null);
        }

        final IMGroupListAdapter.ViewHolder holder = IMGroupListAdapter.ViewHolder.getHolder(convertView);
        //设置数据
        IMContacts friend = list.get(position);
        holder.name.setText(friend.getName());
        holder.rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击设置背景图片 并吧选中的联系人存放起来
                if ("0".equals(holder.ivSelect.getTag())) {
                    holder.ivSelect.setTag("1");
                    //设置成选中状态
                    holder.ivSelect.setImageResource(R.drawable.btn_selected);
                } else {
                    holder.ivSelect.setTag("0");
                    //设置成默认状态
                    holder.ivSelect.setImageResource(R.drawable.btn_unselected);
                }
            }
        });
        char currentWord = friend.getPinyin().charAt(0);
        if (position > 0) {
            //获取上一个item的首字母
            char lastWord = list.get(position - 1).getPinyin().charAt(0);
            //拿当前的首字母和上一个首字母比较
            if (currentWord == lastWord) {
                //说明首字母相同，需要隐藏当前item的first_word
                holder.first_word.setVisibility(View.GONE);
            } else {
                //不一样，需要显示当前的首字母
                //由于布局是复用的，所以在需要显示的时候，再次将first_word设置为可见
                holder.first_word.setVisibility(View.VISIBLE);
                if (Character.isLetter(currentWord)) {
                    holder.first_word.setText(currentWord + "");
                } else {
                    holder.first_word.setVisibility(View.GONE);
                    holder.first_word.setText("#");
                }
            }
        } else {
            holder.first_word.setVisibility(View.VISIBLE);
            if (Character.isLetter(currentWord)) {
                holder.first_word.setText(currentWord + "");
            } else {
                holder.first_word.setText("#");
            }

        }
        return convertView;
    }

    static class ViewHolder {
        TextView name, first_word;
        ImageView ivSelect;
        RelativeLayout rl_item;

        ViewHolder(View convertView) {
            name = convertView.findViewById(R.id.im_tvName);
            first_word = convertView.findViewById(R.id.first_word);
            ivSelect =  convertView.findViewById(R.id.iv_select);
            rl_item = convertView.findViewById(R.id.rl_item);
        }

        static ViewHolder getHolder(View convertView) {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            if (holder == null) {
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }
            return holder;
        }
    }
}
